package com.springstudy.activiti.service;

import com.sun.org.apache.bcel.internal.util.ClassPath;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.task.Task;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.ClassPathResource;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

/**
 * @author chenrongrong
 * @since 2020/3/19 15:28
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class MyLeaveActivitiTest {

    @Autowired
    ProcessEngine processEngine = null;

    @Autowired
    TaskService taskService = null;

    /**
     * 部署流程
     */
    @Test
    public void deployProcess(){
        RepositoryService repositoryService = processEngine.getRepositoryService();
        DeploymentBuilder builder = repositoryService.createDeployment();
        //禁用架构验证，否则会报错
        builder.disableSchemaValidation();
        builder.addClasspathResource("bpmn/leave.bpmn");
        builder.deploy();
    }

    /**
     * 启动流程
     */
    @Test
    public void startProcess(){
        RuntimeService runtimeService = processEngine.getRuntimeService();
        //流程的名称，也可以使用ByID来启动流程
        runtimeService.startProcessInstanceByKey("leave");
    }

    /**
     * 完成节点
     */
    @Test
    public void completeTask() {
        String assignee = "student";
        List<Task> tasks = taskService.createTaskQuery().taskAssignee(assignee).list();

        //循环找出需要完成的节点
        for (Task task : tasks) {
            taskService.complete(task.getId());
        }


        System.out.println("完成节点");
    }

    /**
     * 查询任务节点
     */
    @Test
    public void queryTask(){
        TaskService taskService = processEngine.getTaskService();
        //根据assignee(代理人)查询任务
        String assignee = "student";
        List<Task> tasks = taskService.createTaskQuery().taskAssignee(assignee).list();

        //首次运行的时候这个没有输出，因为第一次运行的时候扫描act_ru_task的表里面是空的，但第一次运行完成之后里面会添加一条记录，之后每次运行里面都会添加一条记录
        for (Task task : tasks) {
            System.out.println("taskId:" + task.getId() +
                    ",taskName:" + task.getName() +
                    ",assignee:" + task.getAssignee() +
                    ",createTime:" + task.getCreateTime());
        }
    }

}
